package com.tkjt.pms.persist.dao.impl;

import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tkjt.pms.calc.common.IndexCalcParallel;
import com.tkjt.pms.common.entity.TkIndexValue;
import com.tkjt.pms.common.entity.TkIndexValueAdjust;
import com.tkjt.pms.common.entity.TkIndexWeight;
import com.tkjt.pms.common.utils.EsBeanUtils;
import com.tkjt.pms.common.utils.EsDateUtils;
import com.tkjt.pms.common.utils.EsJsonUtils;
import com.tkjt.pms.modules.common.mapper.TkIndexValueAdjustMapper;
import com.tkjt.pms.modules.common.mapper.TkIndexValueMapper;
import com.tkjt.pms.modules.manageWeight.mapper.WeightMapper;
import com.tkjt.pms.persist.dao.KpiIndexDao;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Transactional
@Repository("kpiIndexDao")
public class KpiIndexDaoImpl implements KpiIndexDao {

	@Autowired
	private TkIndexValueMapper tkIndexValueMapper;
	@Autowired
	private WeightMapper weightMapper;
	@Autowired
	private TkIndexValueAdjustMapper tkIndexValueAdjustMapper;

	@Override
	public List<TkIndexValue> loadIndexValueList(String indexValueCode, Set<String> orgCodes, Set<String> valueCodes) {
		QueryWrapper<TkIndexValue> objectQueryWrapper = new QueryWrapper<>();
		if (indexValueCode != null && !"".equals(indexValueCode)) {
			objectQueryWrapper.eq("index_value_code", indexValueCode);
		}
		if (orgCodes != null) {
			objectQueryWrapper.in("index_depart", orgCodes);
		}
		if (valueCodes != null) {
			objectQueryWrapper.in("index_value_code", valueCodes);
		}
		List<TkIndexValue> list = tkIndexValueMapper.selectList(objectQueryWrapper);
		return list;
	}

	@Override
	public TkIndexValue getIndexValue(IndexCalcParallel indexCalcParallel) {
		QueryWrapper<TkIndexValue> objectQueryWrapper = new QueryWrapper<>();
		objectQueryWrapper.eq("index_depart", indexCalcParallel.getIndexDepart());
		objectQueryWrapper.eq("index_value_code", indexCalcParallel.getIndexValueCode());
		if (null != indexCalcParallel.getIndexScorer()) {
			objectQueryWrapper.eq("index_scorer", indexCalcParallel.getIndexScorer());
		}
		TkIndexValue entity = tkIndexValueMapper.selectOne(objectQueryWrapper);
		return entity;
	}

	@Override
	public void updateIndexEntity(TkIndexValue entity) {
		QueryWrapper<TkIndexValue> objectQueryWrapper = new QueryWrapper<>();
		objectQueryWrapper.eq("index_depart", entity.getIndexDepart());
		objectQueryWrapper.eq("index_value_code", entity.getIndexValueCode());
		if (null != entity.getIndexExecutor()) {
			objectQueryWrapper.eq("index_executor", entity.getIndexExecutor());
		}
		TkIndexValue dbEntity = tkIndexValueMapper.selectOne(objectQueryWrapper);
		if (null != dbEntity) {
			EsBeanUtils.copyBeanNotNull2Bean(entity, dbEntity);
			tkIndexValueMapper.updateById(dbEntity);
		} else {
			tkIndexValueMapper.insert(entity);
		}
	}

	@Override
	public void updateIndexValues(IndexCalcParallel indexCalcParallel, final Map<String, Double> values) {
		QueryWrapper<TkIndexValue> objectQueryWrapper = new QueryWrapper<>();
		objectQueryWrapper.eq("index_depart", indexCalcParallel.getIndexDepart());
		objectQueryWrapper.eq("index_value_code", indexCalcParallel.getIndexValueCode());
//		if (null != indexExecutor) {
//			objectQueryWrapper.eq("index_executor", indexExecutor);
//		}			
		//特别处理===>重点工作
		String calcIndexValueCode = indexCalcParallel.getIndexValueCode();
		if(calcIndexValueCode.startsWith("KGIAAC002") || calcIndexValueCode.startsWith("KCIAAC002")) {
			objectQueryWrapper.lambda().eq(TkIndexValue::getIndexExecutor, "jiqiongxiao");
		}
		if(StringUtils.isNotEmpty(indexCalcParallel.getDataType())){
			objectQueryWrapper.eq("data_type",indexCalcParallel.getDataType());
		}
		objectQueryWrapper.eq("index_scorer", indexCalcParallel.getIndexScorer());
		TkIndexValue entity = null;
		List<TkIndexValueAdjust> adjusts = null;
		try {
			entity = tkIndexValueMapper.selectOne(objectQueryWrapper);
			if (null == entity) {
				log.error("indexCalcParallel={}, values={}", indexCalcParallel, values);
				return;
			}
			String indexYear = EsDateUtils.getIndexYear(indexCalcParallel.getIndexYearMonth());
			QueryWrapper<TkIndexValueAdjust> adjustQueryWrapper = new QueryWrapper<>();

			adjustQueryWrapper.eq("index_depart", entity.getIndexDepart());
			adjustQueryWrapper.eq("index_value_code", entity.getIndexValueCode());
			if(StringUtils.isNotEmpty(indexCalcParallel.getDataType())){
				adjustQueryWrapper.eq("data_type", indexCalcParallel.getDataType());
			}
			adjustQueryWrapper.eq("year", indexYear);
			adjusts = tkIndexValueAdjustMapper.selectList(adjustQueryWrapper);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			log.error("indexCalcParallel={}, entity={}", indexCalcParallel, entity);
		}
		try {
			Double oneValue = values.get("mTotal");
			if (null != oneValue && isHaveAdjustData(adjusts, "mTotal") == true && !oneValue.isNaN()) {
				entity.setMTotal(oneValue);
			}
			oneValue = values.get("m01");
			if (null != oneValue && isHaveAdjustData(adjusts, "m01") == true && !oneValue.isNaN()) {
				entity.setM01(oneValue);
			}
			oneValue = values.get("m02");
			if (null != oneValue && isHaveAdjustData(adjusts, "m02") == true && !oneValue.isNaN()) {
				entity.setM02(oneValue);
			}
			oneValue = values.get("m03");
			if (null != oneValue && isHaveAdjustData(adjusts, "m03") == true && !oneValue.isNaN()) {
				entity.setM03(oneValue);
			}
			oneValue = values.get("m04");
			if (null != oneValue && isHaveAdjustData(adjusts, "m04") == true && !oneValue.isNaN()) {
				entity.setM04(oneValue);
			}
			oneValue = values.get("m05");
			if (null != oneValue && isHaveAdjustData(adjusts, "m05") == true && !oneValue.isNaN()) {
				entity.setM05(oneValue);
			}
			oneValue = values.get("m06");
			if (null != oneValue && isHaveAdjustData(adjusts, "m06") == true && !oneValue.isNaN()) {
				entity.setM06(oneValue);
			}
			oneValue = values.get("m07");
			if (null != oneValue && isHaveAdjustData(adjusts, "m07") == true && !oneValue.isNaN()) {
				entity.setM07(oneValue);
			}
			oneValue = values.get("m08");
			if (null != oneValue && isHaveAdjustData(adjusts, "m08") == true && !oneValue.isNaN()) {
				entity.setM08(oneValue);
			}
			oneValue = values.get("m09");
			if (null != oneValue && isHaveAdjustData(adjusts, "m09") == true && !oneValue.isNaN()) {
				entity.setM09(oneValue);
			}
			oneValue = values.get("m10");
			if (null != oneValue && isHaveAdjustData(adjusts, "m10") == true && !oneValue.isNaN()) {
				entity.setM10(oneValue);
			}
			oneValue = values.get("m11");
			if (null != oneValue && isHaveAdjustData(adjusts, "m11") == true && !oneValue.isNaN()) {
				entity.setM11(oneValue);
			}
			oneValue = values.get("m12");
			if (null != oneValue && isHaveAdjustData(adjusts, "m12") == true && !oneValue.isNaN()) {
				entity.setM12(oneValue);
			}
			oneValue = values.get("q01");
			if (null != oneValue && isHaveAdjustData(adjusts, "q01") == true && !oneValue.isNaN()) {
				entity.setQ01(oneValue);
			}
			oneValue = values.get("q02");
			if (null != oneValue && isHaveAdjustData(adjusts, "q02") == true && !oneValue.isNaN()) {
				entity.setQ02(oneValue);
			}
			oneValue = values.get("q03");
			if (null != oneValue && isHaveAdjustData(adjusts, "q03") == true && !oneValue.isNaN()) {
				entity.setQ03(oneValue);
			}
			oneValue = values.get("q04");
			if (null != oneValue && isHaveAdjustData(adjusts, "q04") == true && !oneValue.isNaN()) {
				entity.setQ04(oneValue);
			}
			tkIndexValueMapper.updateById(entity);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			log.error("indexCalcParallel={}, entity={}", indexCalcParallel, EsJsonUtils.toJsonStr(entity));
			// throw e;
		}

	}

	public boolean isHaveAdjustData(List<TkIndexValueAdjust> adjusts, String which) {
		boolean sign = true;
		for (TkIndexValueAdjust item : adjusts) {
			if (which.equals(item.getWhich())) {
				sign = false;
			}
		}
		return sign;
	}

	@Override
	public TkIndexWeight getWeight(String indexCode, String indexOrgCode) {
		QueryWrapper<TkIndexWeight> objectQueryWrapper = new QueryWrapper<>();
		objectQueryWrapper.eq("index_code", indexCode);
		objectQueryWrapper.eq("org_code", indexOrgCode);
		TkIndexWeight entity = weightMapper.selectOne(objectQueryWrapper);
		return entity;
	}

	@Override
	public void updateWeightEntity(TkIndexWeight entity) {
		QueryWrapper<TkIndexWeight> objectQueryWrapper = new QueryWrapper<>();
		objectQueryWrapper.eq("index_code", entity.getIndexCode());
		objectQueryWrapper.eq("org_code", entity.getOrgCode());
		TkIndexWeight dbEntity = weightMapper.selectOne(objectQueryWrapper);
		if (null != dbEntity) {
			EsBeanUtils.copyBeanNotNull2Bean(entity, dbEntity);
			weightMapper.updateById(dbEntity);
		} else {
			weightMapper.insert(entity);
		}
	}

}
